package exttable.timetable.models;

import utils.Localisation;

import javax.swing.table.AbstractTableModel;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Apr 11, 2010
 * Time: 3:08:52 PM
 * To change this template use File | Settings | File Templates.
 */

public class VisitTableModel extends AbstractTableModel {
    public static final int VISITS = 0;
    public static final int DAYS = 1;
    public static final int HOURS = 2;
    public static final int MINUETS = 3;

    private int numberOfRows;
    private Vector<VisitModel> data = new Vector<VisitModel>();
    private String[] columnNames = {
            Localisation.getMessage("timetable.visit")
            , Localisation.getMessage("timetable.days")
            , Localisation.getMessage("timetable.hour")
            , Localisation.getMessage("timetable.minuets")
    };

    public VisitTableModel(int numberOfRows) {
        this.numberOfRows = numberOfRows;
        for (int i = 0; i < numberOfRows; i++) {
            VisitModel vm = new VisitModel();
            data.add(vm);
        }
    }


    @Override
    public int getRowCount() {
        return numberOfRows;
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        VisitModel vm = data.get(rowIndex);
        switch (columnIndex) {
            case VISITS:
                return rowIndex;
            case DAYS:
                return vm.getDays();
            case HOURS:
                return vm.getHours();
            case MINUETS:
                return vm.getMinuets();
            default:
                return rowIndex;
        }
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return columnIndex != 0;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        VisitModel vm = data.get(rowIndex);
        String s = aValue.toString();
        switch (columnIndex) {
            case DAYS:
                vm.setDays(Integer.valueOf(s));
                 fireTableCellUpdated(rowIndex, columnIndex);
                break;
            case HOURS:
                vm.setHours(Integer.valueOf(s));
                 fireTableCellUpdated(rowIndex, columnIndex);
                break;
            case MINUETS:
                vm.setMinuets(Integer.valueOf(s));
                 fireTableCellUpdated(rowIndex, columnIndex);
                break;
            default:
                throw new IllegalArgumentException("error");
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
       return Integer.class;
    }
}
